package com.zjw.forum.controller;

import com.sun.istack.internal.NotNull;
import com.zjw.forum.common.AppResult;
import com.zjw.forum.common.ResultCode;
import com.zjw.forum.dao.MessageMapper;
import com.zjw.forum.model.Message;
import com.zjw.forum.model.User;
import com.zjw.forum.services.IMessgaeService;
import com.zjw.forum.services.IUserService;
import com.zjw.forum.utils.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@Api("站内信接口")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    private IMessgaeService messgaeService;
    @Autowired
    private IUserService userService;
    @Autowired
    private MessageMapper messageMapper;
    @PostMapping("/send")
    public AppResult sendMessage(HttpServletRequest request,
                                 @RequestParam("receiveUserId") @NotNull @ApiParam("接收者") Long receiveUserId,
                                 @RequestParam("content") @NotNull @ApiParam("内容")String content){
        //获取发送者的id
        String user_token = request.getHeader("user_token");
        User user = JWTUtil.getUserFromToken(user_token);
        if(user==null){
            //令牌不合法
            log.error(ResultCode.FAILED_TOKEN.getMessage());
            return AppResult.failed(ResultCode.FAILED_TOKEN);
        }
        //判断用户是否存在
        User userinfo1 = userService.getUserinfo(user.getId());
        //判断该用户是否被禁言了，禁言就无法发送信息
        if(userinfo1.getState()==1){
            log.error(ResultCode.FAILED_USER_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //判断接收者是否被禁言了，禁言就无法发送信息
        User userinfo = userService.getUserinfo(receiveUserId);
        if(userinfo.getState()==1){
            log.error(ResultCode.FAILED_USER_OTHERS_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_USER_OTHERS_BANNED);
        }
        //发送者和接受者不能是同一个
        if(user.getId().equals(receiveUserId)){
            log.error(ResultCode.FAILED.getMessage());
            return AppResult.failed(ResultCode.FAILED);
        }
        //开始发送信息
        int i = messgaeService.sendMessage(user.getId(), receiveUserId, content);
        if(i!=1){
            log.warn(ResultCode.FAILED.getMessage());
            return AppResult.failed(ResultCode.FAILED);
        }
        return AppResult.success("发送成功");
    }
    @GetMapping("/getUnreadCount")
    public AppResult getUnreadCount(HttpServletRequest request){
        //获取登陆用户id
        String user_token = request.getHeader("user_token");
        User user = JWTUtil.getUserFromToken(user_token);
        if(user==null){
            //令牌不合法
            log.error(ResultCode.FAILED_TOKEN.getMessage());
            return AppResult.failed(ResultCode.FAILED_TOKEN);
        }
        //判断用户是否存在
        User userinfo = userService.getUserinfo(user.getId());
        return AppResult.success(messgaeService.notReadCount(userinfo.getId()));
    }
    @GetMapping("/getAll")
    public AppResult getAll(HttpServletRequest request){
        //获取登陆用户id
        String user_token = request.getHeader("user_token");
        User user = JWTUtil.getUserFromToken(user_token);
        if(user==null){
            //令牌不合法
            log.error(ResultCode.FAILED_TOKEN.getMessage());
            return AppResult.failed(ResultCode.FAILED_TOKEN);
        }
        //判断用户是否存在
        User userinfo = userService.getUserinfo(user.getId());
        //获取站内信列表
        return AppResult.success(messgaeService.getAll(userinfo.getId()));
    }
    @PostMapping("/markRead")
    public AppResult markRead(
            HttpServletRequest request,
            @RequestParam(value = "id") @ApiParam("站内信id") @NotNull Long id){
        //获取登陆用户id
        String user_token = request.getHeader("user_token");
        User user = JWTUtil.getUserFromToken(user_token);
        if(user==null){
            //令牌不合法
            log.error(ResultCode.FAILED_TOKEN.getMessage());
            return AppResult.failed(ResultCode.FAILED_TOKEN);
        }
        //判断用户是否存在
        User userinfo = userService.getUserinfo(user.getId());

        //获取站内信的接收方id
        Message message = messageMapper.selectByPrimaryKey(id);
        if(message==null){
            log.error(ResultCode.FAILED_NOT_EXISTS.getMessage());
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }

        //判断站内信的接收方是否为登录用户
        if(message.getReceiveUserId()!=userinfo.getId()){
            log.error(ResultCode.FAILED_MESSAGE_BANNED.getMessage());
            return AppResult.failed(ResultCode.FAILED_MESSAGE_BANNED);
        }

        int i = messgaeService.updateState(id);
        if(i!=1){
            log.warn(ResultCode.FAILED.getMessage());
            return AppResult.failed(ResultCode.FAILED);
        }
        return AppResult.success();
    }
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @RequestParam(value = "repliedId") @ApiParam("站内信id") @NotNull Long repliedId,
                           @RequestParam(value = "content") @ApiParam("回复内容") @NotNull String content){
        //获取登陆用户id
        String user_token = request.getHeader("user_token");
        User user = JWTUtil.getUserFromToken(user_token);
        if(user==null){
            //令牌不合法
            log.error(ResultCode.FAILED_TOKEN.getMessage());
            return AppResult.failed(ResultCode.FAILED_TOKEN);
        }
        //判断用户是否存在
        User userinfo = userService.getUserinfo(user.getId());
        int i = messgaeService.replyMessage(userinfo.getId(), repliedId, content);
        if(i!=1){
            log.error(ResultCode.ERROR_SERVICES.getMessage());
            return AppResult.failed(ResultCode.ERROR_SERVICES);
        }
        return AppResult.success();
    }
}
